module;

#include "unit_test/gtest_expand.h"

module infinity_core:ut.stream_reader;

import :ut.base_test;
import :infinity_exception;
import third_party;
import :logger;
import :file_writer;
import :file_reader;
import :infinity_context;
import :stream_reader;
import :virtual_store;

import global_resource_usage;

using namespace infinity;

class StreamReaderTest : public BaseTest {};

TEST_F(StreamReaderTest, TestBasicStreamIO) {
    std::string path = std::string(GetFullTmpDir()) + "/test_streamio.abc";
    FileWriter file_writer(path, 128);

    std::string lines[5];
    lines[0] = "hahahahha";
    lines[1] = "xixixixiix";
    lines[2] = "huhuhuhu";
    lines[3] = "xuxuxuxuxxu";
    lines[4] = "ddddd";

    for (i64 i = 0; i < 5; i++) {
        file_writer.Write(lines[i].c_str(), lines[i].size());
        file_writer.Write("\n", 1);
    }
    file_writer.Sync();

    std::unique_ptr<StreamReader> stream = VirtualStore::OpenStreamReader(path);
    i32 i = 0;
    std::string line;
    while (stream->ReadLine(line)) {
        EXPECT_STREQ(line.c_str(), lines[i].c_str());
        i++;
    }
    EXPECT_EQ(i, 5);
}
